<template>
  <div id="app">
    <a-input :value='inputValue' class="my_ipt" placeholder="请输入任务"  @change='handleInputChange'/>
    <a-button type="primary" @click='addItemToList'>添加事项</a-button>

    <a-list :dataSource="viewList" bordered class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox :checked='item.done' @click='cbStatusChanged($event,item.id)'>{{item.info}}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click='removeItemById(item.id)'>删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{unDoneLength}}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button :type="viewKey=='all'? 'primary':'defult'" @click='changeList("all")'>全部</a-button>
          <!--type默认是"default" 按钮无颜色         -->
          <a-button :type="viewKey=='undone'? 'primary':'defult'" @click='changeList("undone")'>未完成</a-button>
          <a-button :type="viewKey=='done'? 'primary':'defult'" @click='changeList("done")'>已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click='clean'>清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState, mapMutations, mapGetters } from 'vuex'

export default {
  name: 'app',
  data () {
    return {
    }
  },
  created () {
    this.$store.dispatch('getList')
  },
  computed: {
    ...mapState(['list', 'inputValue', 'viewKey']),
    ...mapGetters(['unDoneLength', 'viewList'])
  },
  methods: {
    ...mapMutations(['setInputValue', 'addItem', 'removeItem', 'changeStatus', 'cleanDone', 'changeViewKey']),
    // 监听文本框内容变化
    handleInputChange (e) {
      // console.log(e.target.value)
      this.setInputValue(e.target.value)
    },
    addItemToList () {
      if (this.inputValue.trim().length <= 0) {
        return this.$message.warning('文本框内容不能为空! ')
      }
      this.addItem()
    },
    removeItemById (id) {
      this.removeItem(id)
    },
    cbStatusChanged (e, id) {
      const param = {
        // 鼠标点击选择框更改选择状态,有porps的情况下不更新到attribute
        status: e.target.checked,
        id: id
      }
      this.changeStatus(param)
    },
    // 清除已完成的任务
    clean () {
      this.cleanDone()
    },
    changeList (key) {
      this.changeViewKey(key)
    }
  }
}
</script>

<style scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
